﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace hlavny
{
    [Serializable]
    class cIndRecord : IComparable<modul1.Object>
    {
        private byte[] paKey;

        public byte[] aKey
        {
            get { return paKey; }
            set { paKey = value; }
        }

        private cIndBlock poBlock;

        private long piRowid;

        public long iRowid
        {
            get { return piRowid; }
            set { piRowid = value; }
        }

        private modul1.Object poValue;

        public modul1.Object oValue
        {
            get { return poValue; }
            set { poValue = value; }
        }

        public cIndRecord()
        {
            piRowid = -1;
        }

        public cIndRecord(byte[] pa_aKey, long pa_iRowid, cIndBlock pa_sBlock)
        {
            paKey = pa_aKey;
            piRowid = pa_iRowid;
            poBlock = pa_sBlock;
        }

        public cIndRecord(int pa_iKeyLength, byte[] pa_iInput, cIndBlock pa_sBlock, bool pa_bCreateValue)
        {
            poBlock = pa_sBlock;
            paKey = new byte[pa_iKeyLength];
            Array.Copy(pa_iInput, 0, paKey, 0, pa_iKeyLength);
            piRowid = BitConverter.ToInt64(pa_iInput, pa_iKeyLength);
            // Vytvori sa hodnota - kvoli analyze
            if (pa_bCreateValue == true)
            {
                Type oType = Type.GetType(pa_sBlock.sKeyType, false);
                object[] oArgs;
                if (poBlock.sKeyType == "hlavny.modul1.Character")
                {
                    oArgs = new object[2];
                    oArgs[0] = paKey;
                    oArgs[1] = poBlock.iKeyObjLength;
                }
                else
                {
                    oArgs = new object[1];
                    oArgs[0] = paKey;
                }
                try
                {
                    poValue = (modul1.Object)Activator.CreateInstance(oType, oArgs);
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.InnerException.Message);
                }
            }
        }

        public byte[] toByteArray()
        {
            byte[] aReturn = new byte[paKey.Length + 16];
            Array.Copy(paKey, 0, aReturn, 0, paKey.Length);
            Array.Copy(System.BitConverter.GetBytes(iRowid), 0, aReturn, paKey.Length, 8);
            return aReturn;
        }

        /// <summary>
        /// Porovna dva zaznamy podla klucov
        /// </summary>
        /// <param name="pa_aKey"></param>
        /// <returns></returns>
        public bool equalsKey(byte[] pa_aKey)
        {
            bool bReturn = true;
            if (paKey.Length == pa_aKey.Length)
            {
                for (int ii = 0; ii < paKey.Length; ii++)
                {
                    if (paKey[ii] != pa_aKey[ii])
                    {
                        bReturn = false;
                    }
                }
            }
            else
            {
                return false;
            }
            return bReturn;
        }


        #region IComparable Members

        public int CompareTo(modul1.Object obj)
        {
            return obj.Compare(poValue);
            
        }

        #endregion
    }
}
